//
//  VerticalChangeTabView.m
//  TransportCar
//
//  Created by 张吉旭 on 2017/11/13.
//  Copyright © 2017年 amplity. All rights reserved.
//

#import "VerticalChangeTabView.h"
#import "VerticalChangeTabCollectionViewCell.h"
#import "VerticalChangeTabModel.h"

@interface VerticalChangeTabView()<UICollectionViewDataSource,UICollectionViewDelegate,UITableViewDelegate,UITableViewDataSource>


@property (nonatomic,strong) UICollectionView * listCollectionView;
@property (nonatomic,strong) UITableView * infoTableView;

@property (nonatomic,strong) NSArray * datasSource;

@property (nonatomic,strong) NSMutableArray * tableDatasource;

@end

@implementation VerticalChangeTabView

+(VerticalChangeTabView*)showViewWithSuperView:(UIView*)superView datas:(NSArray*)datas
{
    VerticalChangeTabView *view = [super addSubviewsViewWithSuperView:superView];
    view.backgroundColor = HEXCOLOR(kColorGray7, 1);
    view.datasSource = datas;
    [view setConfig:datas];
    [view makeUI];
    return view;
    

}

-(void)setConfig:(NSArray*)datas{
    _tableDatasource = [[NSMutableArray alloc] init];
}

-(void)makeUI
{

    [self addSubview:self.listCollectionView];
    [self addSubview:self.infoTableView];
    NSIndexPath * indexPath = [NSIndexPath indexPathForItem:0 inSection:0];
    [_listCollectionView selectItemAtIndexPath:indexPath animated:NO scrollPosition:UICollectionViewScrollPositionTop];
    
    
    VerticalChangeTabModel * model = [_datasSource objectAtIndex:indexPath.row];
    [_tableDatasource removeAllObjects];
    [_tableDatasource addObjectsFromArray:model.dataInfoArr];
    [_infoTableView reloadData];
}

-(UICollectionView*)listCollectionView
{
    
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
    flowLayout.minimumLineSpacing = 5;
    flowLayout.minimumInteritemSpacing = 0;
    flowLayout.scrollDirection = UICollectionViewScrollDirectionVertical;
    //    flowLayout.sectionInset = UIEdgeInsetsMake(0, kSpaceSide, 0, 0);
    flowLayout.sectionInset = UIEdgeInsetsMake(5, 5, 0, 5);
    if(_listCollectionView == nil){
        _listCollectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, 100, HEIGHT) collectionViewLayout:flowLayout];
        _listCollectionView.backgroundColor = [UIColor whiteColor];
        [VerticalChangeTabCollectionViewCell registerClassForCollectionView:_listCollectionView];
        _listCollectionView.delegate = self;
        _listCollectionView.dataSource = self;
    }
    
    
    
    return _listCollectionView;
}

-(UITableView*)infoTableView
{
    if (_infoTableView ==nil) {
        _infoTableView = [[UITableView alloc] init];
        _infoTableView.frame = CGRectMake(110, 0, WIDTH-110, HEIGHT);
        _infoTableView.separatorStyle = UITableViewStylePlain;
        _infoTableView.delegate = self;
        _infoTableView.dataSource = self;
    }
    
    return _infoTableView;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return _datasSource.count;
}

// The cell that is returned must be retrieved from a call to -dequeueReusableCellWithReuseIdentifier:forIndexPath:
- (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    VerticalChangeTabCollectionViewCell * cell2 = [collectionView dequeueReusableCellWithReuseIdentifier:[VerticalChangeTabCollectionViewCell reuseIdentifier] forIndexPath:indexPath];
    if(!cell2){
        cell2 = [[VerticalChangeTabCollectionViewCell alloc] initWithFrame:CGRectMake(0, 0, 100, 50)];
        cell2.selectedBackgroundView.backgroundColor = [UIColor redColor];
    }
    
    VerticalChangeTabModel * model = [_datasSource objectAtIndex:indexPath.row];
    
    [cell2 setDataModelForCell:model.title];
    
    return cell2;
}


- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    NSLog(@"点击");
 
    VerticalChangeTabModel * model = [_datasSource objectAtIndex:indexPath.row];
    [_tableDatasource removeAllObjects];
    [_tableDatasource addObjectsFromArray:model.dataInfoArr];
    [_infoTableView reloadData];
}


#pragma mark - UICollectionViewDelegateFlowLayout


- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section
{
    if (section ==0) {
        return CGSizeMake(320, 1);
    }
    return CGSizeMake(320, 10);
}

- (CGSize) collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
    
    return [VerticalChangeTabCollectionViewCell cellSizeAtIndexPath:indexPath];
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForFooterInSection:(NSInteger)section{
    
    if (section ==0) {
        return CGSizeMake(320, 14);
    }
    return CGSizeMake(320, 1);
}


#pragma mark - UITableViewDelegate
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 40;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    
}


#pragma mark - UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _tableDatasource.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSString * tableTitleStr = [_tableDatasource objectAtIndex:indexPath.row];
    
    
    
    UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:@"VerticalChangeTableCell"];
    if(cell==nil){
        cell = [[UITableViewCell alloc] init];
    }
    
    cell.textLabel.text = tableTitleStr;
    return cell;
}

@end
